// pages/sys/user/form/index.js
import {
    request
} from '../../../../request';
import WxValidate from "../../../../utils/WxValidate";
Page({
    data: {
        deptIndex: null,
        sexIndex: null,
        postIndex: null,
        roleIndex: null,
        sexString:null,
        postString:null,
        dept: [],
        sex: [],
        status: true,
        roles: [],
        posts: [],
        form: {
            status: "0",
            password: "123456"
        }
    },
    onLoad: function (options) {
        //获取部门
        request("/system/dept/treeselect", "GET").then(resopnse => {
            this.setData({
                dept: resopnse.data
            });
        })
        //获取角色和职位
        request("/system/user/", "GET").then(resopnse => {
            this.setData({
                posts: resopnse.posts,
                roles: resopnse.roles
            });
        })
        //性别字典
        request("/system/dict/data/type/sys_user_sex", "GET").then(resopnse => {
            this.setData({
                sex: resopnse.data,
            });
        })
        this.initValidate();
        //修改form
        if(options.id!=undefined){
            this.loadFormData(options.id);
        }
    },
    loadFormData(id){
        request("/system/user/"+id,"GET").then(res=>{
            if(res.code ==200){
                var form  = res.data;
                form.roleIds = res.roleIds;
                form.postIds = res.postIds;
                var sta;
                if(form.status =="0"){
                    sta =true;
                }else{
                    sta =false;
                }
                var sex = this.data.sex.filter(item=>item.dictValue == form.sex)[0];
                var post = this.data.posts.filter(item=>item.postId==form.postIds[0])[0];
                this.setData({
                    sexString:sex!=undefined?sex.dictLabel:null,
                    postString:post!=undefined?post.postName:null,
                    status:sta,
                    form:form
                })
            }
        })
        console.log(this.data)
    },
    initValidate() {
        let rules = {
            nickName: {
                required: true
            },
            userName: {
                required: true,
            },
            phone: {
                required: false,
                tel: true,
            },
            email: {
                required: false,
                email: true
            }
        }

        let message = {
            nickName: {
                required: '请输入用户昵称'
            },
            userName: {
                required: "请输入用户名"
            },
            phone: {
                tel: '请输入正确电话'
            },
            email: {
                required: "请输入正确邮箱"
            },
        }
        //实例化当前的验证规则和提示消息
        this.WxValidate = new WxValidate(rules, message);
    },
    deptPickerChange(e) {
        console.log(e);
        var data = this.data.form;
        data["deptId"] = this.data.dept[e.detail.value].id;
        this.setData({
            deptIndex: e.detail.value,
            form: data
        })
    },
    sexPickerChange(e) {
        console.log(e);
        var data = this.data.form;
        data["sex"] = this.data.sex[e.detail.value].dictCode;
        this.setData({
            sexIndex: e.detail.value,
            form: data
        })
    },
    postPickerChange(e) {
        var data = this.data.form;
        var postIds = [];
        postIds.push(this.data.posts[e.detail.value].postId);
        data["postIds"] = postIds;
        this.setData({
            postIndex: e.detail.value,
            form: data
        })
    },
    rolePickerChange(e) {
        var data = this.data.form;
        var roleIds = [];
        roleIds.push(this.data.roles[e.detail.value].roleId);
        data["roleIds"] = roleIds;
        this.setData({
            roleIndex: e.detail.value,
            form: data
        })
    },
    switchStatus() {
        if (this.data.status) {
            this.setData({
                status: false
            })
        } else {
            this.setData({
                status: true
            })
        }
        console.log(this.data.status);
    },
    formSubmit: function (e) {
        console.log(e);
        var formData = this.data.form;
        let params = e.detail.value;
        if (!this.WxValidate.checkForm(params)) {
            let error = this.WxValidate.errorList[0];
            switch (error.param) {
                case "nickName":
                    //TODO
                    break;
                case "userName":
                    //TODO
                    break;
                case "phone":
                    //TODO
                    break;
                case "email":
                    //TODO
                    break;
            }
            console.log(error);
            wx.showToast({
                title: error.msg,
                icon: 'none',
                duration: 2000,
            })
            return false;
        }
        if (params.nickName != "") {
            formData["nickName"] = params.nickName;
        }
        if (params.userName != "") {
            formData["userName"] = params.userName;
        }
        if (params.phone != "") {
            formData["phoneNumber"] = params.phone;
        }
        if (params.email != "") {
            formData["email"] = params.email;
        }
        if (params.note != "") {
            formData["remark"] = params.note;
        }
        this.data.status == true ? formData["status"] = "0" : formData["status"] = "1";
        var status ;
        if(formData.userId!=undefined){
            status = "PUT";
        }else{
            status = "POST";
        }
        request("/system/user", status, formData).then(res => {
            if (res.code == "200") {
                wx.showToast({
                    title: '保存成功',
                    icon: '',
                    duration: 2000,
                })
                wx.navigateBack({
                    delta: 1
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 2000,
                })
            }
        })
        console.log(formData);
    },
})